package com.yunshu.yunshuapp.tongxunlu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yunshu.yunshuapp.OnTapListener;
import com.yunshu.yunshuapp.R;
import com.yunshu.yunshuapp.banner.ScrollBanner;
import com.yunshu.yunshuapp.utils.YSUtils;
import com.yunshu.yunshuapp.zixunlist.ZixunListModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhouwenjie on 2016/12/22.
 */
public class ContactListAdapter extends RecyclerView.Adapter implements OnTapListener {

    private final int RecyclerViewItemTypeCommon = 2;

    private ArrayList<Contact> models;
    public OnTapListener tapListener;
    public Activity activity;

    public ContactListAdapter(Context context){
        // 使用ImageLoader之前初始化
        YSUtils.initImageLoader(context);

        models = new ArrayList<>();

        tapListener = this;
    }

    @Override
    public int getItemCount() {

        int count = models.size();
        return count ;
    }

    @Override
    public int getItemViewType(int position) {

        Contact model = models.get(position);
        return RecyclerViewItemTypeCommon;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tongxunlu_list_item,parent,false);
        ListViewHolder holder = new ListViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        Contact model = models.get(position);
        ((ListViewHolder)holder).setPosition(position);

        ListViewHolder listHolder = (ListViewHolder) holder;
        listHolder.titleTV.setText(model.getName());
    }

    @Override
    public void onSingleTap(View v, int position) {

        Intent intent = new Intent();
        intent.setClass(this.activity, ContactDetailActivity.class);
        Contact model =  this.models.get(position);
        Bundle bundle = new Bundle();
        bundle.putString("contactId",model.getContactId());
        intent.putExtras(bundle);
        this.activity.startActivityForResult(intent,0);
    }

    public void addFooterItem(List<Contact> footerDatas) {
        models.addAll(footerDatas);
        notifyDataSetChanged();
    }

    public void setModels(ArrayList<Contact> models) {
        this.models = models;
    }

    class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView titleTV;

        int mPosition;

        public ListViewHolder(View itemView ) {
            super(itemView);
            itemView.setOnClickListener(this);
            titleTV = (TextView) itemView.findViewById(R.id.titleTV);
        }

        public void setPosition(int position) {
            mPosition = position;
        }


        public void onClick(View v) {
            tapListener.onSingleTap(v,mPosition);
        //    Toast.makeText(MainActivity.this, "click "+mPosition, 0).show();

        }
    }

}
